<?php
	class Baocao extends CI_Controller
	{
		function admin_index()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$loai = $this->uri->segment(3);
				$date1 = $this->uri->segment(4);
				$date2 = $this->uri->segment(5);
				
				$this->load->model('baocao_model');
				
				
				$data['baocaongay']= NULL;
				$data['baocaothang']= NULL;
				$data['baocaonam']= NULL;
				if($loai == 1)
					$data['baocaongay'] = $this->baocao_model->doanhthungay($date1,$date2);
				else
				{
					if($loai == 2)
					{
						$data['baocaothang'] = $this->baocao_model->doanhthuthang($date1,$date2); 
					}
					else
					{
						$data['baocaonam'] = $this->baocao_model->doanhthunam($date1,$date2); //nhà cung cấp đang hợp tác	
					}
				}
				$data['key'] = NULL;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/baocao_view', $data);
			}
			else
				redirect('login/admin_index');
		}
		
		function baocaokhachsan()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$loai = $this->uri->segment(3);
				$loaiks = $this->uri->segment(4);
				$date1 = $this->uri->segment(5);
				$date2 = $this->uri->segment(6);
				
				$this->load->model('baocao_model');
				$data['danhsachks'] = $this->baocao_model->LayDSKS();
				
				
				$data['baocaongay']= NULL;
				$data['baocaothang']= NULL;
				$data['baocaonam']= NULL;
				$data['baocaongayks']= NULL;
				$data['baocaothangks']= NULL;
				$data['baocaonamks']= NULL;
				if($loai == 1) // thống kê theo ngày
				{
					if($loaiks == 0) //tat ca
						$data['baocaongay'] = $this->baocao_model->tatcakhachsanngay($date1,$date2); 
					else
					{
						$data['baocaongayks'] = $this->baocao_model->khachsanngay($date1,$date2,$loaiks);
					}
				}
				else
				{
					if($loai == 2) // thống kê theo tháng
					{
						if($loaiks == 0) //tat ca
						$data['baocaothang'] = $this->baocao_model->tatcakhachsanthang($date1,$date2); 
					
						else
						{
							$data['baocaothangks'] = $this->baocao_model->khachsanthang($date1,$date2,$loaiks);
						}
					}
					else
					{
						if($loaiks == 0) //tat ca
						$data['baocaonam'] = $this->baocao_model->tatcakhachsannam($date1,$date2); 
					
						else
						{
							$data['baocaonamks'] = $this->baocao_model->khachsannam($date1,$date2,$loaiks);
						}	
					}
				}
				$data['key'] = NULL;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/baocaokhachsan_view', $data);
			}
			else
				redirect('login/admin_index');
		}
		
		function download()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$this->load->library('cezpdf');
				$this->load->helper('pdf');
				prep_pdf();	
				$loai = $this->uri->segment(3);
				$date1 = $this->uri->segment(4);
				$date2 = $this->uri->segment(5);
				
				$this->load->model('baocao_model');
				
				
				$data['baocao']= NULL;
				if($loai == 1)
				{
					$data = $this->baocao_model->doanhthungay($date1,$date2);
					$temp1 = 0;
					$temp2 = 0;
					foreach($data as $row)
					{						
						$nam = $row->year;
						$thang = $row->month;
						$ngay = $row->day;
						$doanhthu = number_format($row->doanhthu * 1000, 0, '', '.');
						if($temp1 != $nam)
						{
							$temp1 = $nam;
							$temp2 = $thang;
							$t1 = $nam;
							$t2 = $thang;
							$t3 = $ngay;
							$t4 = $doanhthu;
						}
						else
						{
							$t1 = "";
							if($temp2 != $thang)
							{
								$temp2 = $thang;
								$t2 = $thang;
							}
							else
							{
								$t2 = "";
							}
							$t3 = $ngay;
							$t4 = $doanhthu;			
						}
									
						$db_data[] = array('year' => $t1, 'month' => $t2, 'day' => $t3, 'doanhthu' => $t4);
						
					}
				
					$col_names = array('year' => mb_convert_encoding('Nam', "UTF-16LE", "UTF-8"),'month' => mb_convert_encoding('Thang', "UTF-16LE", "UTF-8"),'day' => mb_convert_encoding('Ngay', "UTF-16LE", "UTF-8"),'doanhthu' => mb_convert_encoding('Doanh thu (VND)', "UTF-16LE", "UTF-8"));
					 
					$this->cezpdf->ezTable($db_data, $col_names, mb_convert_encoding('DOANH THU THEO NGAY', "UTF-16LE", "UTF-8"), array('width'=>550));
					$this->cezpdf->ezStream();

				}
				else
				{
					if($loai == 2)
					{
						$data = $this->baocao_model->doanhthuthang($date1,$date2);
						$temp1 = 0;
						$t = 0;
						foreach($data as $row)
						{						
							$nam = $row->year;
							$thang = $row->month;
							$doanhthu = number_format($row->doanhthu * 1000, 0, '', '.');
							if($temp1 != $nam)
							{
								$t1 = $nam;
								$t2 = $thang;
								$t4 = $doanhthu;
								$temp1 = $nam;
							}
							else
							{
								$t1 = "";
								$t2 = $thang;
								$t4 = $doanhthu;
							}
										
							$db_data[] = array('year' => $t1, 'month' => $t2, 'doanhthu' => $t4);
							
						}
					
	 
						$col_names = array('year' => mb_convert_encoding('Nam', "UTF-16LE", "UTF-8"),'month' => mb_convert_encoding('Thang', "UTF-16LE", "UTF-8"),'doanhthu' => mb_convert_encoding('Doanh thu (VND)', "UTF-16LE", "UTF-8"));
						 
						$this->cezpdf->ezTable($db_data, $col_names, mb_convert_encoding('DOANH THU THEO THANG', "UTF-16LE", "UTF-8"), array('width'=>550));
						$this->cezpdf->ezStream();
					}
					else
					{
						$data = $this->baocao_model->doanhthunam($date1,$date2);
						$temp1 = 0;
						$t = 0;
						foreach($data as $row)
						{						
							$nam = $row->year;
							$doanhthu = number_format($row->doanhthu * 1000, 0, '', '.');
							$db_data[] = array('year' => $nam, 'doanhthu' => $doanhthu);
							
						}
					
	 
						$col_names = array('year' => mb_convert_encoding('Nam', "UTF-16LE", "UTF-8"),'doanhthu' => mb_convert_encoding('Doanh thu (VND)', "UTF-16LE", "UTF-8"));
						 
						$this->cezpdf->ezTable($db_data, $col_names, mb_convert_encoding('DOANH THU THEO NAM', "UTF-16LE", "UTF-8"), array('width'=>550));
						$this->cezpdf->ezStream();
					}
				}
				
			}
			else
				redirect('login/admin_index');
		}
		
		function downloadks()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$this->load->library('cezpdf');
				$this->load->helper('pdf');
				prep_pdf();	
				$loai = $this->uri->segment(3);
				$loaiks = $this->uri->segment(4);
				$date1 = $this->uri->segment(5);
				$date2 = $this->uri->segment(6);
				
				$this->load->model('baocao_model');
				$danhsachks = $this->baocao_model->LayDSKS();
				$dsks = array();
				$i = 0;
				foreach($danhsachks as $row)
				{ 
					$maks = $row->maks;
					$dsks[$i++] = $maks;
				}
				
				if($loai == 1) // thống kê theo ngày
				{
					if($loaiks == 0) //tat ca
					{
						$data = $this->baocao_model->tatcakhachsanngay($date1,$date2); 
						foreach($dsks as $ks)
						{
							$temp1 = 0;
							$temp2 = 0;
							$t = 0;
							foreach($data as $row)
							{
								$maks = $row->maks;
								$tenks = $row->tenks;
								$nam = $row->year;
								$thang = $row->month;
								$ngay = $row->day;
								$doanhthu = number_format($row->doanhthu * 1000, 0, '', '.');
								if($maks == $ks)
								{
									if($temp1 != $nam)
									{
										$temp1 = $nam;
										$temp2 = $thang;
										if($t == 0)
										{
											$t1 = $tenks;
											$t2 = $nam;
											$t3 = $thang;
											$t4 = $ngay;
											$t5 = $doanhthu;
											
											$t = 1;
										}
										else
										{
											$t1 = "";
											$t2 = $nam;
											$t3 = $thang;
											$t4 = $ngay;
											$t5 = $doanhthu;
										}
									}
									else
									{
										$t1 = "";
										$t2 = "";
										
										if($temp2 != $thang)
										{
											$temp2 = $thang;
											$t3 = $thang;
										}
										else
										{
											$t3 = "";
										}
										$t4 = $ngay;
										$t5 = $doanhthu;
									}
									$db_data[] = array('tenks' => $t1, 'year' => $t2, 'month' => $t3, 'day' => $t4, 'doanhthu' => $t5);
								}
							}
						}
						$col_names = array('tenks' => mb_convert_encoding('Khach san', "UTF-16LE", "UTF-8"),'year' => mb_convert_encoding('Nam', "UTF-16LE", "UTF-8"),'month' => mb_convert_encoding('Thang', "UTF-16LE", "UTF-8"),'day' => mb_convert_encoding('Ngay', "UTF-16LE", "UTF-8"),'doanhthu' => mb_convert_encoding('Doanh thu (VND)', "UTF-16LE", "UTF-8"));
					 
						$this->cezpdf->ezTable($db_data, $col_names, mb_convert_encoding('DOANH THU CUA TAT CA KHACH SAN THEO NGAY', "UTF-16LE", "UTF-8"), array('width'=>550));
						$this->cezpdf->ezStream();
						
					}
					else
					{
						$data = $this->baocao_model->khachsanngay($date1,$date2,$loaiks);
						$temp1 = 0;
						$temp2 = 0;
						$t = 0;
						foreach($data as $row)
						{
							$maks = $row->maks;
							$tenks = $row->tenks;
							$nam = $row->year;
							$thang = $row->month;
							$ngay = $row->day;
							$doanhthu = number_format($row->doanhthu * 1000, 0, '', '.');
							if($temp1 != $nam)
							{
								$temp1 = $nam;
								$temp2 = $thang;
								
								$t1 = $nam;
								$t2 = $thang;
								$t3 = $ngay;
								$t4 = $doanhthu;
							}
							else
							{
								$t1 = "";
								if($temp2 != $thang)
								{
									$temp2 = $thang;
									$t2 = $thang;
								}
								else
								{
									$t2 = "";
								}
								$t3 = $ngay;
								$t4 = $doanhthu;
							}
							$db_data[] = array('year' => $t1, 'month' => $t2, 'day' => $t3, 'doanhthu' => $t4);
						}
						$col_names = array('year' => mb_convert_encoding('Nam', "UTF-16LE", "UTF-8"),'month' => mb_convert_encoding('Thang', "UTF-16LE", "UTF-8"),'day' => mb_convert_encoding('Ngay', "UTF-16LE", "UTF-8"),'doanhthu' => mb_convert_encoding('Doanh thu (VND)', "UTF-16LE", "UTF-8"));
					 $title = "DOANH THU CUA KHACH SAN " . $tenks . " THEO NGAY";
					$this->cezpdf->ezTable($db_data, $col_names, mb_convert_encoding($title, "UTF-16LE", "UTF-8"), array('width'=>550));
					$this->cezpdf->ezStream();
					}
				}
				else
				{
					if($loai == 2) // thống kê theo tháng
					{
						if($loaiks == 0) //tat ca
						{
							$data = $this->baocao_model->tatcakhachsanthang($date1,$date2); 
							foreach($dsks as $ks)
							{
								$temp1 = 0;
								$t = 0;

								foreach($data as $row)
								{
									$maks = $row->maks;
									$tenks = $row->tenks;
									$nam = $row->year;
									$thang = $row->month;
									$doanhthu = number_format($row->doanhthu * 1000, 0, '', '.');
									if($ks == $maks)
									{
										if($temp1 != $nam)
										{
											$temp1= $nam;
											if($t == 0)
											{
												$t1 = $tenks;
												$t2 = $nam;
												$t3 = $thang;
												$t4 = $doanhthu;
												$t = 1;
											}
											else
											{
												$t1 = "";
												$t2 = $nam;
												$t3 = $thang;
												$t4 = $doanhthu;
											}
										}
										else
										{
											$t1 = "";
											$t2 = "";
											$t3 = $thang;
											$t4 = $doanhthu;
										}
										$db_data[] = array('tenks' => $t1, 'year' => $t2, 'month' => $t3, 'doanhthu' => $t4);
									}
								}
							}
							$col_names = array('tenks' => mb_convert_encoding('Khach san', "UTF-16LE", "UTF-8"),'year' => mb_convert_encoding('Nam', "UTF-16LE", "UTF-8"),'month' => mb_convert_encoding('Thang', "UTF-16LE", "UTF-8"),'doanhthu' => mb_convert_encoding('Doanh thu (VND)', "UTF-16LE", "UTF-8"));
					 
							$this->cezpdf->ezTable($db_data, $col_names, mb_convert_encoding('DOANH THU CUA TAT CA KHACH SAN THEO THANG', "UTF-16LE", "UTF-8"), array('width'=>550));
							$this->cezpdf->ezStream();
						}
					
						else
						{
							$data = $this->baocao_model->khachsanthang($date1,$date2,$loaiks);
							$temp1 = 0;
							foreach ($data as $row)
							{
								$tenks = $row->tenks;
								$nam = $row->year;
								$thang = $row->month;
								$doanhthu = number_format($row->doanhthu * 1000, 0, '', '.');
								if($temp1 != $nam)
								{
									$t = 1;
									$temp1 = $nam;
									$t1 = $nam;
									$t2 = $thang;
									$t3 = $doanhthu;
								}
								else
								{
									$t1 = "";
									$t2 = $thang;
									$t3 = $doanhthu;
								}
								$db_data[] = array('year' => $t1, 'month' => $t2, 'doanhthu' => $t3);
						}
						$col_names = array('year' => mb_convert_encoding('Nam', "UTF-16LE", "UTF-8"),'month' => mb_convert_encoding('Thang', "UTF-16LE", "UTF-8"),'doanhthu' => mb_convert_encoding('Doanh thu (VND)', "UTF-16LE", "UTF-8"));
					 $title = "DOANH THU CUA KHACH SAN " . $tenks . " THEO THANG";
					$this->cezpdf->ezTable($db_data, $col_names, mb_convert_encoding($title, "UTF-16LE", "UTF-8"), array('width'=>550));
					$this->cezpdf->ezStream();
							
						}
					}
					else
					{
						if($loaiks == 0) //tat ca
						{
							$data = $this->baocao_model->tatcakhachsannam($date1,$date2); 
							foreach ($dsks as $ks)
							{
								$temp = 0;
								$t = 0;
								foreach($data as $row)
								{
									$tenks = $row->tenks;
									$maks = $row->maks;
									$nam = $row->year;
									$doanhthu = number_format($row->doanhthu * 1000, 0, '', '.');
									if($ks == $maks)
									{
										if($t == 0)
										{
											$t = 1;
											$t1 = $tenks;
											$t2 = $nam;
											$t3 = $doanhthu;
										}
										else
										{
											$t1 = "";
											$t2 = $nam;
											$t3 = $doanhthu;
										}
										$temp = $nam;
									$db_data[] = array('tenks' => $t1,'year' => $t2,'doanhthu' => $t3);
									}
								}
							}
								$col_names = array('tenks' => mb_convert_encoding('Khach san', "UTF-16LE", "UTF-8"),'year' => mb_convert_encoding('Nam', "UTF-16LE", "UTF-8"),'doanhthu' => mb_convert_encoding('Doanh thu (VND)', "UTF-16LE", "UTF-8"));
								$title = "DOANH THU TAT CA KHACH SAN THEO NAM";
								$this->cezpdf->ezTable($db_data, $col_names, mb_convert_encoding($title, "UTF-16LE", "UTF-8"), array('width'=>550));
								$this->cezpdf->ezStream();		
							
						}
					
						else
						{
							$data = $this->baocao_model->khachsannam($date1,$date2,$loaiks);
							$temp1 = 0;
							foreach ($data as $row)
							{
								$tenks = $row->tenks;
								$nam = $row->year;
								$doanhthu = number_format($row->doanhthu * 1000, 0, '', '.');
								
								$t1 = $nam;
								$t2 = $doanhthu;
								$db_data[] = array('year' => $t1,'doanhthu' => $t2);
							}
						$col_names = array('year' => mb_convert_encoding('Nam', "UTF-16LE", "UTF-8"),'doanhthu' => mb_convert_encoding('Doanh thu (VND)', "UTF-16LE", "UTF-8"));
					 $title = "DOANH THU CUA KHACH SAN " . $tenks . " THEO NAM";
					$this->cezpdf->ezTable($db_data, $col_names, mb_convert_encoding($title, "UTF-16LE", "UTF-8"), array('width'=>550));
					$this->cezpdf->ezStream();	
							
						}	
					}
				}
				
			}
			else
				redirect('login/admin_index');
		}
		
		public function buku_con()
		{
			parent::__construct();
			$this->load->model('baocao_model');//load the model
			$this->load->library('pagination');
		}
			//function to get the data from tb_book
		function getBuku()
		{
			$data['title'] = 'menampilkan isi buku';
			$data['detail'] = $this->baocao_model->xuatfile();//call the model and save the result in $detail
			$this->load->view('index.php/baocao/admin_index', $data);
		}
		//function to export to excel
		function toExcelAll()
		{
			$query['data1'] = $this->baocao_model->ToExcelAll();
			$this->load->view('index.php/baocao/admin_index',$query);
		}
	}
?>